﻿using System.Text.RegularExpressions;

namespace Magellan.Routing
{
    public class ParameterSegmentRecognizer : RegexBasedSegmentRecognizer
    {
        public ParameterSegmentRecognizer()
            : base(new Regex("^\\{([A-Z0-9]+)\\}$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline))
        {
        }

        protected override Segment Build(Match match, RouteValueDictionary defaults, RouteValueDictionary constraints)
        {
            var placeholder = match.Groups[1].Value;
            return new ParameterSegment(
                placeholder,
                defaults.GetOrDefault<object>(placeholder, UrlParameter.NotSpecified),
                constraints.GetOrDefault<object>(placeholder, UrlParameter.NotSpecified)
                );
        }
    }
}